package com.bbzn.device.client.service.impl.cwting;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.YG;
import com.bbzn.device.client.dto.ct.request.CommandParam;
import com.bbzn.device.client.dto.ct.request.RequestCreateCommand;
import com.bbzn.device.client.dto.ct.request.RequestCreateCommandM2M;
import com.bbzn.device.client.service.emq.OrderSendService;
import com.bbzn.device.client.utils.BabaBeanUtils;
import com.bbzn.device.client.utils.MsgTemplateUtil;
import com.ctg.ag.sdk.biz.AepDeviceCommandLwmProfileClient;
import com.ctg.ag.sdk.biz.aep_device_command.CreateCommandResponse;
import com.ctg.ag.sdk.biz.aep_device_command_lwm_profile.CreateCommandLwm2mProfileRequest;
import com.ctg.ag.sdk.biz.aep_device_command_lwm_profile.CreateCommandLwm2mProfileResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author wulongbo
 * @Date 2021/9/11 11:09
 * @Version 1.0
 */
@Component
public class Lwm2mAndProSendServiceImpl extends OrderSendBaseServiceImpl implements OrderSendService {

    @Override
    public CreateCommandResponse createCommand(String imei, String masterKey, RequestCreateCommand requestCreateCommand, String content) throws Exception {
        // lwm2m协议有profile指令下发接口
        RequestCreateCommandM2M requestCreateCommandM2M = BabaBeanUtils.voToDto(requestCreateCommand, RequestCreateCommandM2M.class);
        //将json对象转换为CommandParam对象
//        CommandParam commandParam = JSONObject.toJavaObject(content, CommandParam.class);
        Map<String, String> replaceMap = new HashMap<String, String>();
        replaceMap.put("deviceid", imei.substring(imei.length() - 12));
        String replaceContent = MsgTemplateUtil.replacePlaceHolderWithMapValue(content, replaceMap);
        JSONObject json = JSONObject.parseObject(replaceContent);
        CommandParam commandParam = JSONObject.toJavaObject(json, CommandParam.class);
        if (commandParam != null) {
            requestCreateCommandM2M.setCommand(commandParam);
        }
        System.out.println("指令内容：" + commandParam.toString());
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestCreateCommandM2M).getBytes();
        AepDeviceCommandLwmProfileClient client = AepDeviceCommandLwmProfileClient.newClient()
                .appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        CreateCommandLwm2mProfileRequest request = new CreateCommandLwm2mProfileRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);    // single value
        request.setBody(body);    //具体格式见前面请求body说明
        CreateCommandLwm2mProfileResponse commandLwm2mProfileResponse = client.CreateCommandLwm2mProfile(request);
        client.shutdown();
        CreateCommandResponse createCommandResponse = BabaBeanUtils.voToDto(commandLwm2mProfileResponse, CreateCommandResponse.class);
        return createCommandResponse;
    }

}
